var co = require('co');
var seqModule = require('../../../routes/createModules');
var seq = new seqModule();
var util = require('../../../common/util');

var videoController = {
    // getVideoWxWQData: function (req, res) {//获取往期视频
    //     var videoSql = "select video.*,(select count(*) from lm_click_log where title_id = video.id) as clickCount,(select count(*) from lm_comment_log where title_id = video.id) as commentCount,(select count(*) from lm_share_log where title_id = video.id) as shareCount from lm_video as video where 1=1";
    //     if (req.query.type != undefined) {
    //         videoSql += " and video.type = " + parseInt(req.query.type);
    //     }
    //     if (req.query.pTitle != undefined) {
    //         videoSql += " and (video.title like '%" + req.query.pTitle + "%' or video.expert_name like '%" + req.query.pTitle + "%')";
    //     } else {
    //         videoSql += " and video.top_time != (select max(video1.top_time) from lm_video as video1)"
    //     }
    //     videoSql += " and video.item  = 1";
    //     videoSql += " order by video.create_time DESC";
    //     seq.sequelize.query(videoSql).spread(function (result) {
    //         res.json({ 'success': true, 'msg': '查询成功！', "result": result });
    //     }).catch(function (err) {
    //         res.json({ 'success': false, 'msg': '查询失败' });
    //         console.log(err);
    //     })

    // },
    getVideoWxMetData: function (req, res) {//获取MET视频
        var videoSql = "select video.*,(select count(*) from lm_click_log where title_id = video.id) as clickCount,(select count(*) from lm_comment_log where title_id = video.id) as commentCount,(select count(*) from lm_share_log where title_id = video.id) as shareCount from lm_video as video where 1=1";
        if (req.query.type != undefined) {
            videoSql += " and video.type = " + parseInt(req.query.type);
        }
        if (req.query.pTitle != undefined) {
            videoSql += " and (video.title like '%" + decodeURI(req.query.pTitle) + "%' or video.expert_name like '%" + decodeURI(req.query.pTitle) + "%')";
        }
        //  else {
        //     videoSql += " and video.top_time != (select max(video1.top_time) from lm_video as video1)"
        // }
        if (req.query.item != undefined) {
            videoSql += " and video.item = " + parseInt(req.query.item);
        }
        videoSql += " order by video.top_time DESC";
        seq.sequelize.query(videoSql).spread(function (result) {
            res.json({ 'success': true, 'msg': '查询成功！', "result": result });
        }).catch(function (err) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(err);
        })
    },
    getTopVideoData: function (req, res) {
        var videoSql = "select * from lm_video as video where video.top_time = (select max(video1.top_time) from lm_video as video1)";
        if (req.query.item != undefined) {
            videoSql += " and video.item = " + parseInt(req.query.item);
        }
        seq.sequelize.query(videoSql).spread(function (result) {
            res.json({ 'success': true, 'msg': '查询成功！', "result": result[0] });
        }).catch(function (err) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(err);
        })
    },
    getIDVideoData: function (req, res) {
        seq.videoModule.findOne({ where: req.params }).then(function (result) {
            res.json({ 'success': true, 'msg': '查询成功！', "result": result });
        }).catch(function (err) {
            res.json({ 'success': false, 'msg': '查询失败' });
            console.log(err);
        })
    }
}
module.exports = videoController;